﻿using Enums;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using XF.Common;
using XF.ExControls;

namespace KYBM
{
    public partial class FrmMaterielList : Form
    {
        XF.BLL.Base_Materiel bll = new XF.BLL.Base_Materiel();
        List<int> lstInsert = new List<int>();
        List<int> lstUpdate = new List<int>();
        int iVerticalScrollIndex = 0;

        public FrmMaterielList()
        {
            InitializeComponent();
        }

        private void FrmMaterielList_Load(object sender, EventArgs e)
        {
            xfDataGridView1.AutoGenerateColumns = false;
            BindData();
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            pagerControl1.PageIndex = 1;
            BindData();
        }

        private void BindData()
        {
            int sum = 0;
            string strWhere = string.Empty;
            if(!string.IsNullOrEmpty(tsTbMateriel.Text.Trim()))
            {
                strWhere += " and (MaterielName like '%" + tsTbMateriel.Text.Trim() + "%' or MaterielModel like '%" + tsTbMateriel.Text.Trim() + "%')";
            }
            List<XF.Model.Base_Materiel> models = bll.GetModelListByPage(strWhere, " MaterielName Asc ", pagerControl1.PageIndex, pagerControl1.PageSize, ref sum);
            pagerControl1.DrawControl(sum);
            xfDataGridView1.Rows.Clear();
            foreach (XF.Model.Base_Materiel model in models)
            {
                int count = xfDataGridView1.RowCount;
                xfDataGridView1.Rows.Add();
                xfDataGridView1.Rows[count].Cells[ColId.Name].Value = model.Id;
                xfDataGridView1.Rows[count].Cells[ColName.Name].Value = model.MaterielName;
                xfDataGridView1.Rows[count].Cells[ColModel.Name].Value = model.MaterielModel;
                xfDataGridView1.Rows[count].Cells[ColCreateDate.Name].Value = model.CreateDate;
            }
            lstInsert.Clear();
            lstUpdate.Clear();
        }
        private void pagerControl1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void tsBtnAdd_Click(object sender, EventArgs e)
        {
            RowAdd();
        }

        private void tsBtnSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }

        private void tsBtnDelete_Click(object sender, EventArgs e)
        {
            RowsDelete();
        }

        /// <summary>
        /// 行数据新增
        /// </summary>
        private void RowAdd()
        {
            xfDataGridView1.Rows.Add();
        }

        /// <summary>
        /// 数据保存
        /// </summary>
        private void SaveData()
        {
            string msg;
            xfDataGridView1.EndEdit();
            if (lstInsert.Count + lstUpdate.Count == 0)
            {
                BindData();
            }
            else
            {
                msg = CheckChangedInput();
                if (msg.Equals(string.Empty))
                {
                    msg += AddData();
                    msg += UpdateData();
                    if (msg.Equals(string.Empty))
                    {
                        QQMessageBox.Show(
                        this,
                        MessageText.TIP_SUCCESS_SAVE,
                        MessageText.MESSAGEBOX_CAPTION_TIP,
                        QQMessageBoxIcon.OK,
                        QQMessageBoxButtons.OK);
                        BindData();
                        xfDataGridView1.FirstDisplayedScrollingRowIndex = iVerticalScrollIndex;
                    }
                    else
                    {
                        QQMessageBox.Show(
                        this,
                        msg,
                        MessageText.MESSAGEBOX_CAPTION_ERROR,
                        QQMessageBoxIcon.Error,
                        QQMessageBoxButtons.OK);
                    }
                }
                else
                {
                    QQMessageBox.Show(
                        this,
                        msg,
                        MessageText.MESSAGEBOX_CAPTION_TIP,
                        QQMessageBoxIcon.Information,
                        QQMessageBoxButtons.OK);
                    return;
                }
            }
        }

        /// <summary>
        /// 新增数据
        /// </summary>
        /// <returns></returns>
        private string AddData()
        {
            string result = string.Empty;
            List<int> successRowIndex = new List<int>();
            XF.Model.Base_Materiel model;
            foreach (int index in lstInsert)
            {
                model = new XF.Model.Base_Materiel();
                model.MaterielName = zDataConverter.ToString(xfDataGridView1.Rows[index].Cells[ColName.Name].Value);
                model.MaterielModel = zDataConverter.ToString(xfDataGridView1.Rows[index].Cells[ColModel.Name].Value);
                int ret = bll.Add(model);
                if (ret == PhysicalConstants.SQL_Existed)
                {
                    result += string.Format(MessageText.SQL_ERROR_CUSTOMER_EXIST, index + 1) + MessageText.KEY_ENTER;
                }
                else
                {
                    xfDataGridView1.Rows[index].Cells[ColId.Name].Value = ret;
                    successRowIndex.Add(index);
                }
            }
            foreach (int rowIndex in successRowIndex)
            {
                lstInsert.Remove(rowIndex);
            }
            return result;
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <returns></returns>
        private string UpdateData()
        {
            string result = string.Empty;
            List<int> successRowIndex = new List<int>();
            XF.Model.Base_Materiel model;
            foreach (int index in lstUpdate)
            {
                model = new XF.Model.Base_Materiel();
                model.Id = zDataConverter.ToInt(xfDataGridView1.Rows[index].Cells[ColId.Name].Value);
                model.MaterielName = zDataConverter.ToString(xfDataGridView1.Rows[index].Cells[ColName.Name].Value);
                model.MaterielModel = zDataConverter.ToString(xfDataGridView1.Rows[index].Cells[ColModel.Name].Value);
                if (bll.Update(model))
                {
                    successRowIndex.Add(index);
                }
                else
                {
                    result += MessageText.SQL_ERROR_UPDATE + MessageText.KEY_ENTER;
                }
            }
            foreach (int rowIndex in successRowIndex)
            {
                lstUpdate.Remove(rowIndex);
            }
            return result;
        }

        /// <summary>
        /// 多行记录删除
        /// </summary>
        private void RowsDelete()
        {
            List<int> indexs = xfDataGridView1.GetSelectedRowsIndex();
            if (indexs.Count > 0)
            {
                string sIDs = string.Empty;
                string sID;
                //构建删除ID
                foreach (int i in indexs)
                {
                    sID = zDataConverter.ToString(xfDataGridView1.Rows[i].Cells[ColId.Name].Value);
                    if (sID.Length > 0)
                    {
                        sIDs += MessageText.KEY_COMMA + sID;
                    }
                }
                if (sIDs.Length > 0)
                {
                    //执行数据库删除
                    if (!bll.DeleteList(sIDs.Substring(1)))
                    {
                        QQMessageBox.Show(
                        this,
                        MessageText.TIP_SUCCESS_DELETE,
                        MessageText.MESSAGEBOX_CAPTION_ERROR,
                        QQMessageBoxIcon.Error,
                        QQMessageBoxButtons.OK);
                        return;
                    }
                }
                BindData();
                xfDataGridView1.FirstDisplayedScrollingRowIndex = iVerticalScrollIndex;
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_SUCCESS_DELETE,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.OK,
                            QQMessageBoxButtons.OK);
            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_NULL,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        /// <summary>
        /// 校验新增和更改的数据
        /// </summary>
        /// <returns></returns>
        private string CheckChangedInput()
        {
            string checkResult = string.Empty;
            string result;
            foreach (int rowIndex in lstInsert)
            {
                result = CheckInput(rowIndex);
                if (!result.Equals(string.Empty))
                {
                    checkResult += result + MessageText.KEY_ENTER;
                }
            }
            foreach (int rowIndex in lstUpdate)
            {
                result = CheckInput(rowIndex);
                if (!result.Equals(string.Empty))
                {
                    checkResult += result + MessageText.KEY_ENTER;
                }
            }
            return checkResult;
        }

        /// <summary>
        /// 数据校验
        /// </summary>
        /// <returns>校验结果信息，为空表示校验通过</returns>
        private string CheckInput(int rowIndex)
        {
            if (zDataConverter.ToString(xfDataGridView1.Rows[rowIndex].Cells[ColName.Name].Value).Equals(string.Empty))
            {
                return string.Format(MessageText.CHECK_ERROR_MATERIEL_NAME, rowIndex + 1);
            }
            return string.Empty;
        }

        private void SaveChangedRowIndex(int rowIndex)
        {
            //记录新增和修改行
            if (zDataConverter.ToString(xfDataGridView1.Rows[rowIndex].Cells[ColId.Name].Value).Equals(string.Empty))
            {
                if (!lstInsert.Contains(rowIndex))
                {
                    lstInsert.Add(rowIndex);
                }
            }
            else
            {
                if (!lstUpdate.Contains(rowIndex))
                {
                    lstUpdate.Add(rowIndex);
                }
            }
        }

        private void xfDataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            SaveChangedRowIndex(e.RowIndex);
        }

        private void xfDataGridView1_Scroll(object sender, ScrollEventArgs e)
        {
            if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
            {
                iVerticalScrollIndex = e.NewValue;
            }
        }
    }
}
